<template>
	<view class="container">
		<!-- 主体部分 -->
		<view class="catalog">
			<view>
				<text>推送消息</text>
				<switch :checked="checked0" class="switch" @change="handleChange($event, 0)" color="#FFCC33" />
			</view>
			<view>
				<text>语音播报</text>
				<switch :checked="checked1" class="switch" @change="handleChange($event, 1)" color="#FFCC33" />
			</view>
			<view v-if="userType==0">
				<text>接受邀请</text>
				<switch :checked="checked2" class="switch" @change="handleChange($event, 2)" color="#FFCC33" />
			</view>
			<view v-if="userType==1" style="position: relative;">
				<view style="position: absolute;left:18%;top:9%">
					<uni-tooltip :styleType="5" :styleType2="1" :styleType3="8"
						content="开工审核开启后，雇员需向您申请开工，待您同意后雇员才可以开始工作">
						<image style="width: 54rpx;height: 100%;" src="../../static/helpwen.png" mode="widthFix">
						</image>
					</uni-tooltip>
				</view>
				<text>开工审核</text>
				<switch :checked="checked3" class="switch" @change="handleChange($event, 3)" color="#FFCC33" />
			</view>
			<view @click="handleClearCache">
				<text>清除缓存</text>
				<view>
					<view>{{ storageSize }}M</view>
					<image src="/static/icon1/17.png" mode="aspectFill"></image>
				</view>
			</view>
			<view @click="handleOpenPage(0)">
				<text>意见反馈</text>
				<view>
					<image @click="handleOpenComplain" src="/static/icon1/17.png" mode="aspectFill"></image>
				</view>
			</view>
			<view @click="handleOpenPage(1)">
				<text>关于我们</text>
				<view>
					<image src="/static/icon1/17.png" mode="aspectFill"></image>
				</view>
			</view>
			<view @click="handleOpenPage(2)">
				<text>{{ detailService }}</text>
				<view>
					<image src="/static/icon1/17.png" mode="aspectFill"></image>
				</view>
			</view>
			<!-- <view @click="cancellation">
				<text>账户注销</text>
				<view>
					<image src="/static/icon1/17.png" mode="aspectFill"></image>
				</view>
			</view> -->
			<view @click="handleOpenPage(3)">
				<text>{{detailPrivacyT}}</text>
				<view>
					<image src="/static/icon1/17.png" mode="aspectFill"></image>
				</view>
			</view>
			<view>
				<text>版本号</text>
				<view>
					<text style="color: rgba(64, 33, 16, 0.5);">{{version}}</text>
					<!-- <image src="/static/icon1/17.png" mode="aspectFill"></image> -->
				</view>
			</view>
			<!-- <view @click="handleOpenPage(4)">
				<text>切换身份</text>
				<view>
					<text style="color: rgba(64, 33, 16, 0.5);">转雇主</text>
					
				</view>
			</view> -->
		</view>
		<view class="button" @click="handleExitApp">安全退出</view>
	</view>
</template>
<script>
	export default {
		data() {
			return {
				detailPrivacyT: '隐私政策', //隐私标题
				detailService: '服务协议', //服务
				storageSize: 0,
				userType: uni.getStorageSync('userType'),
				checked0: true, //开关是否开着
				checked1: true, //开关是否开着
				checked2: true, //开关是否开着
				checked3: false, //开关是否开着
				userInfo: {
					user: {},
					params: {}
				}, //当前用户信息
				version: '0.0.0'
			}
		},
		methods: {
			cancellation() {
				uni.showModal({
					title: '提示',
					content: '确定是否注销账号？',
					success: (res) => {
						if (res.confirm) {
							this.logOutUser();
						} else if (res.cancel) {
							console.log('用户点击取消');
						}
					}
				})
			},
			//注销账户
			async logOutUser() {
				let res = await this.$fetch(this.$api.logOutUser, uni.getStorageSync('token'), 'POST')
				if (res.code == 0) {
					uni.removeStorageSync('userInfo');
					uni.removeStorageSync('token');
					uni.removeStorageSync('userType');
					uni.removeStorageSync('userId');
					uni.removeStorageSync('level');
					uni.removeStorageSync('first-is');
					uni.removeStorageSync('isHavePayPassword');
					uni.showToast({
						title: '账户已注销',
						icon: 'none'
					})
					setTimeout(() => {
						uni.switchTab({
							url: '/pages/index/index'
						})
					}, 600)
				} else {
					uni.showToast({
						title: res.msg,
						icon: 'none'
					})
				}
			},
			// 获取隐私文章详情
			async getArticleDetailP() {
				let res = await this.$fetch(this.$api.get_article_by_id, {
					id: 4
				}, 'POST')
				if (res.code) return uni.showToast({
					title: res.msg,
					icon: 'none'
				})
				this.detailPrivacyT = res.data.articleTitle
			},
			// 获取服务详情
			async getArticleDetailS() {
				let res = await this.$fetch(this.$api.get_article_by_id, {
					id: 3
				}, 'POST')
				if (res.code) return uni.showToast({
					title: res.msg,
					icon: 'none'
				})
				this.detailService = res.data.articleTitle
				// console.log(this.detailService)
			},
			// 打开意见反馈
			handleOpenComplain() {
				uni.navigateTo({
					url: '/pages/tousu/boss/recommendation'
				})
			},
			//开关变化
			async handleChange(e, flag) {
				if (flag === 0) {
					this.checked0 = e.detail.value === 'false' || !e.detail.value ? false : true
					let pinf = plus.push.getClientInfo();
					let cidNow = pinf.clientid; //客户端标识  
					let cid = this.checked0 == 0 ? 0 : cidNow
					console.log('cid', cid)
					let res = await this.$fetch(this.$api.upd_user_detail, {
						cid: cid
					}, 'POST')
					if (res.code) return uni.showToast({
						title: res.msg,
						icon: 'none'
					})
				}
				if (flag === 1) {
					console.log(e.detail.value, 777777)
					uni.setStorageSync('checked1', e.detail.value)
					console.log('播报语音设置', uni.getStorageSync('checked1'))
				}
				if (flag === 2) {
					this.checked2 = e.detail.value === 'false' || !e.detail.value ? false : true
					let isInvite = this.checked2 ? 1 : 0
					let res = await this.$fetch(this.$api.upd_user_detail, {
						isInvite: isInvite
					}, 'POST')
					if (res.code) return uni.showToast({
						title: res.msg,
						icon: 'none'
					})
				}
				if (flag == 3) {
					this.checked3 = e.detail.value === 'false' || !e.detail.value ? false : true
					let res = await this.$fetch(this.$api.automatic_check_switch, {}, 'POST')
					if (res.code) return uni.showToast({
						title: res.msg,
						icon: 'none'
					})
					// console.log('关闭开工审核')
				}
				this.getUserInfo()
			},
			//清除缓存
			handleClearCache() {
				uni.showModal({
					title: '提示',
					content: '确定清除缓存吗?',
					success: (res) => {
						// 用户确定要删除
						if (res.confirm) {
							uni.showToast({
								title: '清除成功~',
								icon: 'none'
							})
							this.storageSize = 0
							uni.setStorageSync('storageSize', 0)
							uni.setStorageSync('employerTelephone', '')
						}
					}
				})
			},
			// 打开内页面
			handleOpenPage(page) {
				if (page == 0) {
					uni.navigateTo({
						url: '/pages/tousu/boss/recommendation'
					})
				} else if (page == 1) {
					uni.navigateTo({
						url: '/pages/about-us/about-us'
					})
				} else if (page == 2) {
					uni.navigateTo({
						url: '/pages/user-agreement/service-agreement'
					})
				} else if (page == 3) {
					uni.navigateTo({
						url: '/pages/user-agreement/privacy-agreement'
					})
				} else if (page == 4) {
					uni.navigateTo({
						url: '/pages/change-identity/change-identity'
					})
				}
			},
			// 关闭推送
			async exitCloseCid() {
				let res = await this.$fetch(this.$api.upd_user_detail, {
					cid: 0
				}, 'POST')
				if (res.code) return uni.showToast({
					title: res.msg,
					icon: 'none'
				})
				console.log('关闭推送', res)
				this.getUserInfo()
			},
			// 获取当前用户信息
			async getUserInfo() {
				let res = await this.$fetch(this.$api.get_current_user, {}, 'GET')
				// if (res.code) return uni.showToast({
				// 	title: res.msg,
				// 	icon: 'none'
				// })
				if (res.code === 531) {
					uni.navigateTo({
						url: '/pages/riskControl/riskControl',
					})
					return
				}
				if (res.data.payPwdFlag) {
					uni.setStorageSync('isHavePayPassword', true)
				} else {
					uni.setStorageSync('isHavePayPassword', false)
				}
				console.log('获取当前用户信息', res.data)
				uni.setStorageSync('userInfo', res.data)
				uni.setStorageSync("level", res.data.user.level) //用户等级
			},
			//安全退出
			handleExitApp() {
				// 关闭推送
				this.exitCloseCid()
				uni.showToast({
					title: '已退出~',
					icon: 'none'
				})
				try {
					uni.removeStorageSync('userInfo');
					uni.removeStorageSync('token');
					uni.removeStorageSync('userType');
					uni.removeStorageSync('userId');
					uni.removeStorageSync('level');
					uni.removeStorageSync('first-is');
					uni.removeStorageSync('isHavePayPassword');
					uni.removeStorageSync('guhzuTime')
				} catch (e) {
					// error
				}
				uni.$emit('userLogin', {
					online: 0
				})
				uni.$emit('closeWorking', {})
				uni.$emit('closeDaikgong', {})
				uni.$emit('closeFjin', {})
				// try {
				// 	uni.clearStorageSync();
				// } catch (e) {
				// 	console.log(e)
				// }
				// uni.setStorageSync('location', [])
				setTimeout(() => {
					uni.reLaunch({
						url: '/pages/index/index'
					})
					// location.reload()
				}, 600)
			},
			// 每天显示的随机缓存
			showStorage() {
				this.version = uni.getStorageSync('version')
				this.storageSize = uni.getStorageSync('storageSize') ? uni.getStorageSync('storageSize') : 0
				let x = Math.round(Math.random() * 10) / 30
				uni.setStorageSync('storageSize', this.storageSize + x)
				this.storageSize = (this.storageSize + x).toFixed(1)
			},
			// 获取当前用户信息
			async getUserInfo() {
				let res = await this.$fetch(this.$api.get_current_user, {}, 'GET')
				if (res.code) return
				// 整理数据,res.data.user为空，则改为""
				Object.getOwnPropertyNames(res.data.user).forEach(function(key) {
					res.data.user[key] = res.data.user[key] == null ? '' : res.data.user[key]
				})
				if (res.data.payPwdFlag) {
					uni.setStorageSync('isHavePayPassword', true)
				} else {
					uni.setStorageSync('isHavePayPassword', false)
				}
				uni.setStorageSync('userInfo', res.data)
				this.userInfo = res.data
				this.checked0 = this.userInfo.cid == 0 ? false : true //是否推送消息
				this.checked2 = this.userInfo.isInvite ? true : false //是否接受邀请
				this.checked3 = this.userInfo.taskAutoAudit == 0 ? false : true //是否开工审核
			},
		},
		onLoad() {
			this.getUserInfo()
			this.getArticleDetailS()
			this.getArticleDetailP()
			// #ifdef APP-PLUS
			this.showStorage()
			this.checked1 = uni.getStorageSync('checked1')
			// uni.setStorageSync('version', (plus.runtime.version).split('.').join('.'))
			// #endif
			// console.log('版本',uni.getStorageSync('version') )
		},
		onShow() {
			this.checked1 = uni.getStorageSync('checked1')
			console.log(uni.getStorageSync('checked1'), 789)
			this.getUserInfo()
		}
	}
</script>
<style lang="less">
	.container {
		overflow: hidden;

		// border: 1px solid #000000;
		.catalog {
			margin-top: 30rpx;
			background-color: #FFFFFF;

			&>view {
				padding: 0 30rpx;
				position: relative;
				display: flex;
				align-items: center;
				justify-content: space-between;

				&>text {
					height: 40rpx;
					margin: 30rpx 0;
				}

				&>.switch {
					transform: scale(0.9);
					position: absolute;
					right: 15rpx;
				}

				&>view {
					display: flex;
					align-items: center;

					&>view {
						margin-right: 30rpx;
						color: rgba(64, 33, 16, 0.50);
						letter-spacing: 0;
					}

					&>image {
						width: 16rpx;
						height: 26rpx;
					}
				}

				&::after {
					content: "";
					position: absolute;
					top: 100rpx;
					width: 720rpx;
					height: 2rpx;
					background: #F2F2F2;
				}
			}
		}

		&>.button {
			margin-top: 30rpx;
			width: 100vw;
			height: 100rpx;
			background: #FFFFFF;
			display: flex;
			justify-content: center;
			align-items: center;
			color: #E02020;
			letter-spacing: 0;

			&:active {
				opacity: 0.7;
			}
		}
	}
</style>